<?php

namespace Crm\Controller;

/**
 * 商品控制器
 * 
 * @author SoChishun <14507247@qq.com>
 * @since 1.0.0 <2015-3-27> SoChishun Added.
 */
class ProductController extends AppbaseController {

    /**
     * 检测用户访问权限
     * @since 1.0 <2015-3-27> SoChishun Added.
     */
    public function assert_upr($rule = '') {
        $upr = array(
            'product_list' => true,
            'product_edit' => true,
            'product_delete'=>true,
        );

        $this->user_permission_rules = $upr;
        $this->assign('upr', $this->user_permission_rules); // 声明页面权限变量
        if ($rule && (!array_key_exists($rule, $upr) || !$upr[$rule])) {
            die('您没有访问权限!');
        }
    }

    /**
     * 获取product_list查询表单数据
     * @return array 包含search和where数组
     * @since 1.0 <2015-3-27> SoChishun Added.
     */
    function product_list_search() {
        $search = I('get.');
        if ($search['keyword']) {
            $where['product_name'] = array('like', '%' . $search['keyword'] . '%');
        }
        return array('search' => $search, 'where' => $where);
    }

    /**
     * product_list操作
     * @since 1.0.0 <2015-3-27> SoChishun Added.
     */
    public function product_list() {
        $this->assert_upr(ACTION_NAME); // 检测访问权限
        $this->base_common_add_visit_history('商品管理'); // 添加用户浏览历史
        $search_data = $this->product_list_search(); // 获取查询表单数据
        $list = D('Product')->get_paging_list($search_data['where'], $pager);
        $this->assign('list', $list);
        $this->assign('pager', $pager);
        $this->display_tpp();
    }

    /**
     * product_edit操作
     * @param int $id 主键编号
     * @since 1.0 <2015-3-27> SoChishun Added.
     */
    public function product_edit($id = 0) {
        $this->assert_upr(ACTION_NAME); // 检测访问权限
        $this->base_common_add_visit_history($id ? '编辑商品' : '新增商品'); // 添加用户浏览历史
        $this->assign('ds_category', D('ProductCategory')->get_tree_list('treeselect'));
        $this->base_common_edit_view($id, 'Product');
    }

    /**
     * product_edit_save操作
     * @since 1.0 <2015-3-27> SoChishun Added.
     */
    public function product_edit_save() {
        $result = D('Product')->save_item();
        $this->base_redirect_return($result, U('Product/product_list'));
    }

    /**
     * product_delete操作
     * @param int $id 主键编号
     * @since 1.0 <2015-3-27> SoChishun Added.
     */
    public function product_delete($id = 0) {
        $this->base_common_delete($id, 'Product');
    }
    
    public function product_log($id=0){
        
    }

}
